package com.shd.edanpei.ui.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.presenter.contract.HouseNeedDataContract;
import com.shd.edanpei.presenter.HouseNeedDataPresenter;
import com.shd.edanpei.view.ViewEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 住房订单 订单所需材料1
 */

@SuppressWarnings("ALL")
public class HouseNeedDataActivity1 extends BaseActivity<HouseNeedDataPresenter> implements HouseNeedDataContract.HouseNeedDataIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.et_houseSize)
    EditText etHouseSize;
    @BindView(R.id.tv_houseNature)
    TextView tvHouseNature;
    @BindView(R.id.tv_houseAge)
    TextView tvHouseAge;
    @BindView(R.id.tv_houseStatus)
    TextView tvHouseStatus;
    @BindView(R.id.tv_houseAddress)
    TextView tvHouseAddress;
    @BindView(R.id.et_detailedAddress)
    ViewEditText etDetailedAddress;
    private int orderID;
    private int from = -1;

    @Override
    protected int setLayout() {
        return R.layout.activity_houseneeddata1;
    }

    @Override
    protected HouseNeedDataPresenter bindPresenter() {
        return new HouseNeedDataPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);

        Bundle extras = getIntent().getExtras();
        from = extras.getInt(Constant.FROM);
        if (from == 1) {
            headline.setText("修改订单所需材料");
            orderID = extras.getInt(Constant.ORDER_ID);
            mPresenter.getOrderInfo(orderID);
            CustomerBean customerBean=extras.getParcelable("customerBean");
            //回带数据到此页面
            if(customerBean!=null)
            setAddressAndHouse(customerBean);
        } else {
            headline.setText("订单所需材料");
            CustomerBean customerBean = extras.getParcelable(Constant.DATA);
            //回带数据到此页面
            if(customerBean!=null)
            setAddressAndHouse(customerBean);
        }



    }
    private void setAddressAndHouse(CustomerBean customerBean){
        tvHouseAddress.setText(customerBean.getAddress());
        customerBean.setRoomArea(customerBean.getAddress());

        etDetailedAddress.setText(customerBean.getDetailAddress());
        etDetailedAddress.setClearIconVisible(true);
        customerBean.setHouseDetailAddress(customerBean.getDetailAddress());
        //往bean里面添加默认数据
        mPresenter.setCustomerBean(customerBean);

    }



    @Override
    protected void initListener() {
        etHouseSize.addTextChangedListener(new TextWatcher() {

            boolean deleteLastChar;// 是否需要删除末尾

            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString().contains(".")) {
                    // 如果点后面有超过三位数值,则删掉最后一位
                    int length = s.length() - s.toString().lastIndexOf(".");
                    // 说明后面有三位数值
                    deleteLastChar = length >= 4;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s == null) {
                    return;
                }
                if (deleteLastChar) {
                    // 设置新的截取的字符串
                    etHouseSize.setText(s.toString().substring(0, s.toString().length() - 1));
                    // 光标强制到末尾
                    etHouseSize.setSelection(etHouseSize.getText().length());
                }
                // 以小数点开头，前面自动加上 "0"
                if (s.toString().startsWith(".")) {
                    etHouseSize.setText("0" + s);
                    etHouseSize.setSelection(etHouseSize.getText().length());
                }
                mPresenter.setHouseSize(s.toString());
            }
        });
        etHouseSize.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText editText1 = (EditText) v;
                // 以小数点结尾，去掉小数点
                if (!hasFocus && editText1.getText() != null && editText1.getText().toString().endsWith(".")) {
                    etHouseSize.setText(editText1.getText().subSequence(0, editText1.getText().length() - 1));
                    etHouseSize.setSelection(etHouseSize.getText().length());
                }
            }
        });
        etDetailedAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mPresenter.setDetailedAddress(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBank() {
        finish();
    }

    /**
     * 选择住房性质
     */
    @OnClick(R.id.rl_houseNature)
    void onHouseNature() {
        mPresenter.setHouseNature(tvHouseNature);
    }

    /**
     * 选择房龄
     */
    @OnClick(R.id.rl_houseAge)
    void onHouseAge() {
        mPresenter.setHouseAge(tvHouseAge);
    }

    /**
     * 选择住房现状
     */
    @OnClick(R.id.rl_houseStatus)
    void onHouseStatus() {
        mPresenter.setHouseStatus(tvHouseStatus);
    }

    /**
     * 选择住房所在地
     */
    @OnClick(R.id.rl_houseAddress)
    void onHouseAddress() {
        mPresenter.setHouseAddress(tvHouseAddress);
    }

    @OnClick(R.id.tv_next)
    void onNext() {
        mPresenter.next(from);

    }


    @Override
    public void setText(TextView text, String str) {
        text.setText(str);
        text.setTextColor(ContextCompat.getColor(this, R.color.black));
    }

    @Override
    public void setData(CustomerBean bean) {
        if (!TextUtils.isEmpty(bean.getRoomSize())) etHouseSize.setText(bean.getRoomSize());
        if (!TextUtils.isEmpty(bean.getRoomType())) setText(tvHouseNature, bean.getRoomType());
        if (!TextUtils.isEmpty(bean.getRoomAge()))  setText(tvHouseAge, bean.getRoomAge());
        if (!TextUtils.isEmpty(bean.getRoomStatus()))   setText(tvHouseStatus, bean.getRoomStatus());
        if (!TextUtils.isEmpty(bean.getRoomArea())) setText(tvHouseAddress, bean.getRoomArea());
        if (!TextUtils.isEmpty(bean.getHouseDetailAddress()))   etDetailedAddress.setText(bean.getHouseDetailAddress());

    }


    @Subscribe(code = RxBusCode.NEED_DATA_SUBMIT_SUCCESS)
    public void doneNeedData(CustomerBean bean) {
        finish();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

}
